package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TZhgdDeviceToolsStorageMapper;
import com.ruoyi.system.domain.TZhgdDeviceToolsStorage;
import com.ruoyi.system.service.ITZhgdDeviceToolsStorageService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工器具明细Service业务层处理
 * 
 * @author wuyafeng
 * @date 2025-01-16
 */
@Service
public class TZhgdDeviceToolsStorageServiceImpl implements ITZhgdDeviceToolsStorageService 
{
    private static final Logger log = LoggerFactory.getLogger(TZhgdDeviceToolsStorageServiceImpl.class);
    @Autowired
    private TZhgdDeviceToolsStorageMapper tZhgdDeviceToolsStorageMapper;

    /**
     * 查询工器具明细
     * 
     * @param id 工器具明细主键
     * @return 工器具明细
     */
    @Override
    public TZhgdDeviceToolsStorage selectTZhgdDeviceToolsStorageById(String id)
    {
        return tZhgdDeviceToolsStorageMapper.selectTZhgdDeviceToolsStorageById(id);
    }

    /**
     * 查询工器具明细列表
     * 
     * @param tZhgdDeviceToolsStorage 工器具明细
     * @return 工器具明细
     */
    @Override
    public List<TZhgdDeviceToolsStorage> selectTZhgdDeviceToolsStorageList(TZhgdDeviceToolsStorage tZhgdDeviceToolsStorage)
    {
        return tZhgdDeviceToolsStorageMapper.selectTZhgdDeviceToolsStorageList(tZhgdDeviceToolsStorage);
    }

    /**
     * 新增工器具明细
     * 
     * @param tZhgdDeviceToolsStorage 工器具明细
     * @return 结果
     */
    @Override
    public int insertTZhgdDeviceToolsStorage(TZhgdDeviceToolsStorage tZhgdDeviceToolsStorage)
    {
        tZhgdDeviceToolsStorage.setCreateTime(DateUtils.getNowDate());
        return 1;
    }

    /**
     * 修改工器具明细
     * 
     * @param tZhgdDeviceToolsStorage 工器具明细
     * @return 结果
     */
    @Override
    public int updateTZhgdDeviceToolsStorage(TZhgdDeviceToolsStorage tZhgdDeviceToolsStorage)
    {
        tZhgdDeviceToolsStorage.setUpdateTime(DateUtils.getNowDate());
        return tZhgdDeviceToolsStorageMapper.updateTZhgdDeviceToolsStorage(tZhgdDeviceToolsStorage);
    }

    /**
     * 批量删除工器具明细
     * 
     * @param ids 需要删除的工器具明细主键
     * @return 结果
     */
    @Override
    public int deleteTZhgdDeviceToolsStorageByIds(String ids)
    {
        return tZhgdDeviceToolsStorageMapper.deleteTZhgdDeviceToolsStorageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工器具明细信息
     * 
     * @param id 工器具明细主键
     * @return 结果
     */
    @Override
    public int deleteTZhgdDeviceToolsStorageById(String id)
    {
        return tZhgdDeviceToolsStorageMapper.deleteTZhgdDeviceToolsStorageById(id);
    }

    @Override
    public List<TZhgdDeviceToolsStorage> toolsStorageNoSync() {
        return tZhgdDeviceToolsStorageMapper.selectToolsStorageNoSync();
    }
    /**
     * 上传工器具明细
     *
     * @return 结果
     */
    @Async
    @Override
    public String uploadToolsStorage(String url,String projectId){
        List<TZhgdDeviceToolsStorage> toolsStorage = toolsStorageNoSync();
       if(toolsStorage==null||toolsStorage.size()==0){
           log.info("toolsStorage no data to upload...");
           return "toolsStorage no data to upload...";
       }
        toolsStorage.forEach(x -> {
            x.setProjectId(projectId);
        });
        JSONObject obj = new JSONObject();
        obj.put("toolsStorage",JSON.toJSON(toolsStorage).toString());
        try {
            String result = HttpUtils.postCall(url,obj.toString());
            JSONObject jsonData = JSONObject.parseObject(result);
            if("success".equals(jsonData.get("state"))){
                toolsStorage.forEach( storage->{
                    storage.setSyncFlag(1);
                    tZhgdDeviceToolsStorageMapper.updateTZhgdDeviceToolsStorage(storage);
                });
            }
            log.info("请求结果"+result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "工器具类型同步任务执行";
    }
}
